function addChild(btn) {
	var form = $($(btn).parents(".form").parent().children(".form")[0]);
	var id = getField(form, "id");
	createFrame();
 	setData($("#address_adder"), { parent_id: id });
}
function addBrother(btn) {
	var form = $($(btn).parents(".form").parent().children(".form")[0]);
	var parentId = getField(form, "parent_id");
	createFrame();
 	setData($("#address_adder"), { parent_id: parentId });
}
function callback(form, result, data) {
	var treeView = $("#dataTable");
	var item = treeView.find("li[id=" + data.parent_id + "]")[0];
	result.data.label = result.data.name;
	if (!item) {
		treeView.jqxTree("addTo", result.data);
	} else {
		item = treeView.jqxTree("getItem", item);
		treeView.jqxTree("addTo", result.data, item);
	}
}
function deleteCallback(form, result, data) {
	var treeView = $("#dataTable");
	var item = treeView.find("li[id=" + data.id + "]")[0];
	treeView.jqxTree("removeItem", item);
	clearData(form);
	$("#address-editer input[type=button]").attr("disabled", "disabled");
}
function createFrame() {
 	_editerIndex = layer.open({
		id: "sellerEditer",
		title: "增加地址信息",
	    type: 1,
	    skin: "layui-layer-rim",
	    area: ["720px", "500px"],
	    content: 
	    '<div id="address_adder" class="form" style="height: 100%;overflow: auto;">' +
	    '<input type="hidden" name="submit-url" value="address/add.do" />' +
		'<input type="hidden" name="parent_id" />' +
		'<input type="hidden" name="ing" />' +
		'<input type="hidden" name="lat" />' +
		'<label class="from-label" for="name">名称：</label>' +
		'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="名称"/>' +
		'<label class="from-label" for="pinyin">拼音：</label>' +
		'<input type="text" name="pinyin" allowBlank="false" class="form-control" placeholder="拼音" />' +
		'<label class="from-label" for="city_code">地区编码：</label>' +
		'<input type="text" name="city_code" allowBlank="false" class="form-control" placeholder="地区编码" />' +
		'<label class="from-label" for="zip_code">邮政编码：</label>' +
		'<input type="text" name="zip_code" allowBlank="false" class="form-control" placeholder="邮政编码" />' +
		'</div>' +
		'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
		'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this, true, callback)" value="保存"/>' +
		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
		'</div>',
	});
}
$(function(){
	var treeView = $("#dataTable");
	$.ajax({
		async: false,
		url: "address/tree.do",
		success: function(data, status) {
        	map(data.data, "name", "label");
        	for(var i = 0; i < data.data.length; i++){
        		data.data[i].items = [{ label: "Loading..." }];
        	}
			treeView.jqxTree({source: data.data, width: "23%", height: 650});
			treeView.on("expand", function(event){
				var label = treeView.jqxTree("getItem", event.args.element).label;
				var element = $(event.args.element);
				var loader = false;
	            var loaderItem = null;
	            var children = element.find("ul:first").children();
	            children.each(function(index, item){
	            	item = treeView.jqxTree("getItem", item);
	            	if(item && item.label == "Loading..."){
	            		loaderItem = item;
	            		loader = true;
	            		return false;
	            	}
	            });
                if (loader) {
                    $.ajax({
                		url: "address/tree.do",
                		data: {id : loaderItem.parentId },
                        success: function (data, status) {
                        	map(data.data, "name", "label");
                        	for(var i = 0; i < data.data.length; i++){
                        		data.data[i].items = [{ label: "Loading..." }];
                        	}
                        	treeView.jqxTree("addTo", data.data, element[0]);
                        	children.each(function(index, item) {
                            	treeView.jqxTree("removeItem", item);
                        	});
                        }
                    });
                }
			});
			treeView.on("select", function(event){
				$.ajax({
					url: "address/get.do",
					data: { id: event.args.owner.selectedItem.id },
					success: function(data, status){
						$("#address-editer input[type=button]").attr("disabled", null);
						if(data && data.data){
							setData($("#address-editer"), data.data);
						}
					}
				});
			});
		}
	});
	$("#address-editer input[type=button]").attr("disabled", "disabled");
});